<?php
include_once('prepend.inc.php');
include_once('../Panel/pnlHeader.php');
include_once('../libs/Security/encrypt.class.php');


class Register extends QForm
{
	public $txtFullName;
	protected $txtUserName;
	protected $txtPassword;
	protected $txtConfirmPass;
	protected $blnActive;
	protected $btnRegister;
	protected $btnCheckAvail;
	protected $btnCancel;
	protected $mctMemberdetails;
	protected $txtMemberId;
	protected $objMember;
	protected $objConfirmation;
	protected $pnlHeader;
	
	protected function Form_Run()
	{
		QApplicationBase::CheckRemoteAdmin();
	}
	
	protected function Form_Create()
	{
		QApplication::$LoadedPage = 'Registeration';
		$this->pnlHeader_Create();
		$this->txtConfirmPass_Create();
		$this->btnCheckAvail_Create();
		$this->btnCancel_Create();
		$this->MemberDetails_Create();
	}
	
	protected function pnlHeader_Create()
	{
		$this->pnlHeader = new pnlHeader($this, 'CloseEditPane');
	}
	
	protected function txtConfirmPass_Create()
	{
		$this->txtConfirmPass = new QTextBox($this);
		$this->txtConfirmPass->Name = 'Retype Password';
		$this->txtConfirmPass->TextMode = QTextMode::Password;
	}
	
	protected function btnCheckAvail_Create()
	{
		$this->btnCheckAvail = new QButton($this);
		$this->btnCheckAvail->Text = 'Check Availability';
		$this->btnCheckAvail->SetCustomStyle('margin-left','73%');
		$this->btnCheckAvail->AddAction(new QClickEvent(),new QServerAction('CheckAvailability_Click'));
	}
	
	protected function btnCancel_Create()
	{
		$this->btnCancel = new QButton($this);
		$this->btnCancel->Text = 'Cancel';
		$this->btnCancel->AddAction(new QClickEvent(),new QServerAction('Cancel_Click'));
	}
	
	protected function Cancel_Click()
	{
		QApplication::Redirect('../index.php');		
	}
	
	protected function CheckAvailability_Click()
	{
		
		$this->CheckAvailabilityOfUserId();
	}
	
	protected function CheckAvailabilityOfUserId($CallFromValidation = null)
	{
		if($this->txtMemberId->Text <> '')
			{
				$objcondition = QQ::Equal(QQN::Memberdetails()->MemberId,$this->txtMemberId->Text);
				$cnt = Memberdetails::QueryCount($objcondition);
				
				if($cnt == 0)
				{
					if(isset($CallFromValidation))
					{
						$this->txtMemberId->Instructions = 'User is Available.';
						return true;
					}
					else
					{
						$this->txtMemberId->Instructions = 'User is Available.';
						return true;
						//QApplicationBase::DisplayAlert('User is Available');
					}
				}
				else
				{
					$this->txtMemberId->Warning = 'User Id is not available'.
					$this->txtMemberId->Instructions = '';
					//QApplicationBase::DisplayAlert('User is not Available');
					return false;
				}
			}
			else
			{
				$this->txtMemberId->Focus();
				QApplicationBase::DisplayAlert('Please choose a User Name');
			}
	}
	protected function Register_Click($strFormId, $strControlId, $strParameter)
	{
		$encrypt = new encrypt();
		$this->txtPassword->Text = $encrypt->encrypt_sha1($this->txtPassword->Text);
		$this->objMember = new Memberdetails();
		
		$this->objMember->FullName = $this->txtFullName->Text;
		$this->objMember->MemberId = $this->txtMemberId->Text;
		$this->objMember->Password = $this->txtPassword->Text;
		$this->objMember->Active = false;
		 
		$this->objMember->Save();
		QApplicationBase::Redirect('index.php');
	}
	
	protected function MemberDetails_Create()
	{
		// Use the CreateFromPathInfo shortcut (this can also be done manually using the MemberdetailsMetaControl constructor)
		// MAKE SURE we specify "$this" as the MetaControl's (and thus all subsequent controls') parent
		$this->mctMemberdetails = MemberdetailsMetaControl::CreateFromPathInfo($this);

		// Call MetaControl's methods to create qcontrols based on Memberdetails's data fields
		$this->txtMemberId = $this->mctMemberdetails->txtMemberId_Create();
		$this->txtFullName = $this->mctMemberdetails->txtFullName_Create();
		$this->txtPassword = $this->mctMemberdetails->txtPassword_Create();

		// Create Buttons and Actions on this Form
		$this->btnRegister = new QButton($this);
		$this->btnRegister->Text = QApplication::Translate('Register');
		$this->btnRegister->AddAction(new QClickEvent(), new QServerAction('Register_Click'));
		$this->btnRegister->CausesValidation = true;
	}
	
	
	/**
	 * This Form_Validate event handler allows you to specify any custom Form Validation rules.
	 * It will also Blink() on all invalid controls, as well as Focus() on the top-most invalid control.
	 */
	protected function Form_Validate() 
	{
		// By default, we report that Custom Validations passed
		$blnToReturn = true;

		// Custom Validation Rules
		if(!$this->CheckAvailabilityOfUserId('X'))
			$blnToReturn = false;
		/*Check for the match of passwords here*/
		if($this->txtConfirmPass->Text <> $this->txtPassword->Text)
		{
			$this->txtPassword->Warning = 'The Passwords are not matching';
			$blnToReturn = false;
		}
		// TODO: Be sure to set $blnToReturn to false if any custom validation fails!

		$blnFocused = false;
		foreach ($this->GetErrorControls() as $objControl) {
			// Set Focus to the top-most invalid control
			if (!$blnFocused) {
				$objControl->Focus();
				$blnFocused = true;
			}

			// Blink on ALL invalid controls
			$objControl->Blink();
		}

		return $blnToReturn;
	}
	
}

Register::Run('Register');
?>